<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function register()
	{
		if(!$this->session->userdata('login_member')){
			$this->load->view('register.php');
		}
		else{
			$this->load->view('home.php');
		}
		
	}
	
	public function register_process()
	{
		$this->load->database();
		$this->load->helper('recaptcha_helper');
		$this->load->model('daftar_akun_pengguna','',TRUE);
		$this->form_validation->set_rules('fullname', 'Full Name', 'required');
		$this->form_validation->set_rules('username', 'Username', 'required|callback_check_username');
		$this->form_validation->set_rules('email_address', 'Email Address', 'required|valid_email|callback_check_email');
		$this->form_validation->set_rules('city', 'City', 'required');
		$this->form_validation->set_rules('country', 'Country', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
		$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->register();
		}
		else
		{
			$datebirth = $this->input->post('datebirth');
				$akun_pengguna = array('nama' 			=> $this->input->post('fullname'),
									'username' 			=> $this->input->post('username'),
									'tanggal_lahir'		=> $datebirth,
									'alamat_email' 		=> $this->input->post('email_address'),
									'kota' 				=> $this->input->post('city'),
									'negara' 			=> $this->input->post('country'),
									'password' 			=> $this->input->post('password'),
									'reset_password' 	=> md5($this->input->post('passconf')),
									'jenis_kelamin'		=> $this->input->post('sex'),
							);
			//$username = $this->input->post('username_field');
			//$email = $this->input->post('email_field');			
				$privatekey = '6Lds_b8SAAAAAHR-pCEvuLOzMb5mlPRg_hZzSpeh' ;
				$resp = recaptcha_check_answer ($privatekey,
                                $this->input->server('REMOTE_ADDR'),
                                $this->input->post('recaptcha_challenge_field'),
                                $this->input->post('recaptcha_response_field'));

				if (!$resp->is_valid) {
					// What happens when the CAPTCHA was entered incorrectly
					$this->session->set_flashdata('message','maaf, anda salah memasukkan captcha');
					//$this->register();
					redirect('auth/register');
				}
				
				else
				{
					$this->daftar_akun_pengguna->insert($akun_pengguna);
					//$nama = $this->daftar_akun_pengguna->data_login($username)->nama;
					//$id_pengguna = $this->daftar_akun_pengguna->data_login($username)->id_akun_pengguna;
					//$data = array('nama' => $nama, 'login_member' => TRUE, 'id_akun_pengguna' => $id_pengguna);
					//$this->session->set_userdata($data);
					//redirect('member');
					//$this->load->view('home');				
					echo 'berhasil input data';
				}	
		}
	}
	
	public function check_username()
	{
		$username = $this->input->post('username_field');
		if($this->daftar_akun_pengguna->check_username($username) == FALSE)
		{			
			//$this->session->set_flashdata('message','sorry, username is not available');
			//$this->register();			
			$this->form_validation->set_message('check_username', 'username is not available');
			return FALSE;
		}
	}
	
	public function check_email()
	{
		$email = $this->input->post('email_field');
		if($this->daftar_akun_pengguna->check_email($email)==FALSE)
		{
			//$this->session->set_flashdata('message','sorry, email address is not available');
			//$this->register();
			$this->form_validation->set_message('check_email', 'email address is not available');
			return FALSE;
		}
	}
	
	public function login()
	{
		$this->load->view('login.php');
	}
	
	public function login_process()
	{
		$this->load->database();
		$this->load->model('daftar_akun_pengguna','',TRUE);
		$this->form_validation->set_rules('password', 'Password', 'required');
		$this->form_validation->set_rules('username', 'Username', 'required');
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		
		if($this->form_validation->run()==TRUE)
		{
			if($this->daftar_akun_pengguna->check_login_member($username,$password)==FALSE)
			{
				$this->session->set_flashdata('message', 'Sorry, username and password is incorrect');
			}else{
				$nama = $this->daftar_akun_pengguna->data_login_member($username)->nama;
				$id_pengguna = $this->daftar_akun_pengguna->data_login_member($username)->id_akun_pengguna;
				$data = array('nama' => $nama, 'login_member' => TRUE, 'id_akun_pengguna' => $id_pengguna);
				$this->session->set_userdata($data);
				redirect('home');
				//echo 'anda berhasil login';
			}					
		}else{
			$this->login();
		}
	}
	
	public function logout()
	{
		$this->session->sess_destroy();
		redirect('welcome');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
